<?php

/**
 * This is the model class for table "category_news".
 *
 * The followings are the available columns in table 'category_news':
 * @property integer $id
 * @property string $parent_id
 * @property string $title
 * @property integer $sort_order
 * @property integer $status
 */
class Category extends CActiveRecord
{
    const ENABLED = 1;
    const DISABLED = 0;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return CategoryNews the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'category';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('title', 'required'),
            array('id, sort_order, status', 'numerical', 'integerOnly' => true),
            array('parent_id', 'length', 'max' => 10),
            array('title', 'length', 'max' => 300),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, parent_id, title, sort_order, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'parent' => array(self::BELONGS_TO, 'Category', 'parent_id'),
            'children' => array(self::HAS_MANY, 'Category', 'parent_id'),
            'totalChildren' => array(self::STAT, 'Category', 'parent_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
            'parent_id' => 'Chuyên mục cha',
            'title' => 'Tiêu đề',
            'sort_order' => 'Thứ tự hiển thị',
            'status' => 'Trạng thái',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('parent_id', $this->parent_id, true);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('sort_order', $this->sort_order);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Danh sách các trạng thái có thể có của một banner
     * @return array
     */
    public function getListStatus()
    {
        return array(
            '0' => Yii::t('admin', 'Disabled'),
            '1' => Yii::t('admin', 'Enabled'),
        );
    }

    public function getStatusName($status)
    {
        $arr = $this->getListStatus();
        return $arr[$status];
    }
}